package com.dx.mes.service.brm.service;

import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_ADD;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_DELETE;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_QUERY;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_UPDATE;
import static com.dx.mes.common.bean.def.GenericDef.E_READ_NOT_FOUND;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_MESG_OK;
import static com.dx.mes.common.bean.def.GenericDef.TRX_OUT;
import static com.dx.mes.common.bean.def.GenericDef.T_FBPMDLDEF;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.common.table.ppt.Bis_bom;
import com.dx.common.table.ppt.Bis_bomId;
import com.dx.common.table.ppt.Bis_layot;
import com.dx.common.table.ppt.Bis_mdl_def;
import com.dx.common.table.ppt.Bis_mdl_grd;
import com.dx.common.table.ppt.Ret_wo;
import com.dx.mes.common.dao.IBisBomDao;
import com.dx.mes.common.dao.IBisDataDao;
import com.dx.mes.common.dao.IBisLayotDao;
import com.dx.mes.common.dao.IBisMdlDefDao;
import com.dx.mes.common.dao.IBisMdlGrdDao;
import com.dx.mes.common.dao.IRetPrdInfoDao;
import com.dx.mes.common.dao.IRetWODao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.GUIDGenerator;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.brm.service.IFbpmdldefService;
import com.dx.mes.facade.brm.tx.Fbpmdldef.FbpmdldefI;
import com.dx.mes.facade.brm.tx.Fbpmdldef.FbpmdldefIA;
import com.dx.mes.facade.brm.tx.Fbpmdldef.FbpmdldefIB;
import com.dx.mes.facade.brm.tx.Fbpmdldef.FbpmdldefIC;
import com.dx.mes.facade.brm.tx.Fbpmdldef.FbpmdldefO;
import com.dx.mes.facade.brm.tx.Fbpmdldef.FbpmdldefOA;
import com.dx.mes.facade.brm.tx.Fbpmdldef.FbpmdldefOB;

@Service("fbpmdldefService")
public class FbpmdldefService implements IFbpmdldefService{
	private static final String TX_NAME = T_FBPMDLDEF;
	private LogUtils logUtils;
	
	@Autowired
	private IBisMdlDefDao bisMdlDefDao;

	@Autowired
	private IBisBomDao bisBomDao;

	@Autowired
	private IBisMdlGrdDao bisMdlGrdDao;
	
	@Autowired
	private IBisDataDao bisDataDao;

	@Autowired
	private IBisLayotDao bisLayotDao;
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	@Autowired
	private IRetWODao retWODao;
	
	public String subMainProc(String evt_no,String strInMsg) {
		AppContext.clear();
		AppContext.setCurrServiceName(FbpmdldefService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FbpmdldefService.class);
		logUtils.info("[InTrx:" + strInMsg + "]");
		
		Session session = bisMdlDefDao.getCrSession();
		FbpmdldefO outTrx = new FbpmdldefO();
		String strOutTrx = null;
		
		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));		
		
		try {
			FbpmdldefI inTrx = new FbpmdldefI();

			inTrx = JacksonUtil.fromJson(strInMsg, FbpmdldefI.class);
			char action_flg = inTrx.getAction_flg().charAt(0);
			switch (action_flg) {
			case ACTION_FLG_QUERY:
				queryByCondition(inTrx,outTrx);
				break;
			case ACTION_FLG_ADD:
				addFnc(inTrx,outTrx);
				break;
			case ACTION_FLG_UPDATE:
				updateFnc(inTrx,outTrx);
				break;
			case ACTION_FLG_DELETE:
				deleteFnc(inTrx,outTrx);
				break;
			case 'M':
				queryGradeGroup(inTrx,outTrx);
				break;
			case 'N':
				queryPrdGrade(inTrx,outTrx);
				break;
			case 'P':
				queryExistWoIdByPath(inTrx,outTrx);
				break;
			default:
				errActionFnc(outTrx);
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				session.flush();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			strOutTrx = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + strOutTrx + "]");
		}
		return strOutTrx;
	}

	public void addFnc(FbpmdldefI inTrx,FbpmdldefO outTrx) {
		FbpmdldefIA iary = inTrx.getIary().get(0);
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		// check exit
		Bis_mdl_def bismdl = bisMdlDefDao.get(iary.getMdl_id());

		if (bismdl != null) {
			outTrx.setRtn_code("301234");
			outTrx.setRtn_mesg("[" + iary.getMdl_id() + "]已经存在!请确认！");
			return;
		}
		//
		Bis_mdl_def bisMdlDef = new Bis_mdl_def();
		bisMdlDef.setMdl_id(iary.getMdl_id());
		bisMdlDef.setMdl_cate(iary.getMdl_cate());
		bisMdlDef.setMdl_dsc(iary.getMdl_dsc());
		bisMdlDef.setMdl_size(iary.getMdl_size());

		bisMdlDef.setBox_cnt(iary.getBox_cnt());
		//bisMdlDef.setCus_mdl_id(iary.getCus_mdl_id());
		//bisMdlDef.setFrom_thickness( Double.valueOf(iary.getFrom_thickness() ) );
		//bisMdlDef.setLayot_id_fk(iary.getLayot_id_fk());
		//bisMdlDef.setOk_ratio(iary.getOk_ratio());
		bisMdlDef.setPath_id_fk(iary.getPath_id_fk());
		bisMdlDef.setPath_ver_fk(iary.getPath_ver_fk());
		bisMdlDef.setSwh_time(iary.getSwh_time());
		//bisMdlDef.setTo_thickness( Double.valueOf(iary.getTo_thickness()) );
		//bisMdlDef.setCus_id(iary.getCus_id());
		bisMdlDef.setMax_pf_cnt(iary.getMax_pf_cnt());
		bisMdlDef.setValid_flg(iary.getValid_flg());
		//改为内包数量上限
		bisMdlDef.setBox_cnt(iary.getMax_rwk_cnt());
		//bisMdlDef.setMax_rwk_cnt(iary.getMax_rwk_cnt());
		bisMdlDef.setPost_flag(iary.getPost_flag());
		bisMdlDef.setEvt_usr(inTrx.getEvt_usr());
		bisMdlDef.setEvt_timestamp(current_timestamp);
		bisMdlDefDao.save(bisMdlDef);
		
		/**
		 * add bom information
		 */
		List<FbpmdldefIB> iaryBList = iary.getIaryB();
		for(FbpmdldefIB iaryB : iaryBList){
			Bis_bomId id = new Bis_bomId();
			Bis_bom bis_bom = new Bis_bom();
			id.setMdl_id_fk(iaryB.getMdl_id_fk());
			id.setMtrl_prod_id_fk(iaryB.getMtrl_prod_id_fk());
			id.setMdl_mtrl_seq_id(GUIDGenerator.javaGUID());
			bis_bom.setId(id);
			bis_bom.setMtrl_cate(iaryB.getMtrl_cate());
			bis_bom.setMtrl_unit_fk(iaryB.getMtrl_unit_fk());
			bis_bom.setMtrl_use_qty(iaryB.getMtrl_use_qty());
			bis_bom.setCns_flg(iaryB.getCns_flg());
			bis_bom.setEvt_usr(inTrx.getEvt_usr());
			bis_bom.setEvt_timestamp(current_timestamp);
			bisBomDao.save(bis_bom);
		}
		
		/*add level information*/
		List<FbpmdldefIC> iaryCList = iary.getIaryC();
		for (FbpmdldefIC iaryC : iaryCList) {
			String  evtSeqId = GUIDGenerator.javaGUID();
			Bis_mdl_grd bis_mdl_grd = new Bis_mdl_grd();
			String cusId = iary.getCus_id();
			String prdGrade = iaryC.getPrd_grade();
			bis_mdl_grd.setEvt_seq_id(evtSeqId);
			bis_mdl_grd.setCus_id_fk(cusId);
			bis_mdl_grd.setMdl_typ_fk(iary.getMdl_cate());
			bis_mdl_grd.setMdl_id_fk(iary.getMdl_id());
			bis_mdl_grd.setLow_ng_limt(iaryC.getLow_ng_limt());
			bis_mdl_grd.setUp_ng_limt(iaryC.getUp_ng_limt());
			//根据cus_id和prd_Grdae得到GDGP
//			String gdGrp= getGdGrp(cusId,prdGrade);
//			bis_mdl_grd.setGd_grp(gdGrp);
			bis_mdl_grd.setPrd_grade(prdGrade);
			bis_mdl_grd.setEvt_usr(inTrx.getEvt_usr());
			bis_mdl_grd.setEvt_timestamp(current_timestamp);
			bisMdlGrdDao.save(bis_mdl_grd);
		}
		
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	public void updateFnc(FbpmdldefI inTrx,FbpmdldefO outTrx) {
		int i =0,j=0;
		FbpmdldefIA iary = inTrx.getIary().get(0);
		List<FbpmdldefIB> iaryBList = iary.getIaryB();
		List<FbpmdldefIC> iaryCList = iary.getIaryC();
		String mdlId = iary.getMdl_id();
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		Bis_mdl_def bisMdlDef = bisMdlDefDao.get(mdlId);
		if (bisMdlDef == null) {
			outTrx.setRtn_code(E_READ_NOT_FOUND + "");
			outTrx.setRtn_mesg("找不到该产品代码");
			return;
		}

		if (iary.getMdl_dsc() != null) {
			bisMdlDef.setMdl_dsc(iary.getMdl_dsc());
		}
		if (iary.getMdl_cate() != null) {
			bisMdlDef.setMdl_cate(iary.getMdl_cate());
		}
		if(iary.getLayot_id_fk() != null){
			bisMdlDef.setLayot_id_fk(iary.getLayot_id_fk());
		}
		if (iary.getMdl_size() != null) {
			bisMdlDef.setMdl_size(iary.getMdl_size());
		}
		if (iary.getPath_id_fk() != null) {
			bisMdlDef.setPath_id_fk(iary.getPath_id_fk());
		}
		if (iary.getPath_ver_fk() != null) {
			bisMdlDef.setPath_ver_fk(iary.getPath_ver_fk());
		}

		if (iary.getBox_cnt() != null) {
			bisMdlDef.setBox_cnt(iary.getBox_cnt());
		}
		if (iary.getCus_mdl_id() != null) {
			bisMdlDef.setCus_mdl_id(iary.getCus_mdl_id());
		}
		if (iary.getOk_ratio() != null) {
			bisMdlDef.setOk_ratio(iary.getOk_ratio());
		}
		if (iary.getFrom_thickness() != null) {
			bisMdlDef.setFrom_thickness( Double.valueOf(iary.getFrom_thickness() ) );
		}
		if (iary.getTo_thickness() != null) {
			bisMdlDef.setTo_thickness( Double.valueOf(iary.getTo_thickness()) );
		}
		if (iary.getSwh_time() != null) {
			bisMdlDef.setSwh_time(iary.getSwh_time());
		}
		if (iary.getValid_flg() != null) {
			bisMdlDef.setValid_flg(iary.getValid_flg());
		}
		if (iary.getCus_id() != null) {
			bisMdlDef.setCus_id(iary.getCus_id());
		}
		if(iary.getMax_pf_cnt()!=null){
			bisMdlDef.setMax_pf_cnt(iary.getMax_pf_cnt());
		}
		if(iary.getMax_rwk_cnt()!=null){
			//改为内包数量
			bisMdlDef.setBox_cnt(iary.getMax_rwk_cnt());
			//bisMdlDef.setMax_rwk_cnt(iary.getMax_rwk_cnt());
		}
		if(iary.getPost_flag()!=null){
			bisMdlDef.setPost_flag(iary.getPost_flag());
		}
		bisMdlDef.setEvt_usr(inTrx.getEvt_usr());
		bisMdlDef.setEvt_timestamp(current_timestamp);
		bisMdlDefDao.update(bisMdlDef);

		/**
		 * update bom information
		 */
		String hql = "FROM Bis_bom where mdl_id_fk='" + mdlId+"'";
		List<Bis_bom> bisBomList = bisBomDao.find(hql);
		
		/**
		 * delete no need port info
		 */
		/*if(bisBomList.size()>0){
			for(i=0; i<bisBomList.size(); i++){
				Bis_bom bis_bom = bisBomList.get(i);
				for(j=0; j<bisBomList.size(); j++){
					FbpmdldefIB iaryB = inTrx.getIary().get(0).getIaryB().get(j);
					if( bis_bom.getId().getMdl_id_fk().trim().equals(mdlId.trim())
							&& bis_bom.getId().getMtrl_prod_id_fk().trim().equals(iaryB.getMtrl_prod_id_fk().trim()) 
							&& bis_bom.getId().getMdl_mtrl_seq_id().trim().equals(iaryB.getMdl_mtrl_seq_id().trim())){
						break;
					}
				}
				if(j>=iaryBList.size()){
					bisBomDao.delete(bis_bom);
				}
			}
		}*/
		
		if(bisBomList.size() > 0){
			List<FbpmdldefIB> iaryB = inTrx.getIary().get(0).getIaryB();
			for(i = 0;i < bisBomList.size(); i++){
				Bis_bom bis_bom = bisBomList.get(i);
				if(iaryB.isEmpty()){
					bisBomDao.delete(bis_bom);
				}else{
					for(j=0; j<iaryB.size(); j++){
						if(bis_bom.getId().getMdl_id_fk().trim().equals(mdlId.trim())
								&& bis_bom.getId().getMtrl_prod_id_fk().trim().equals(iaryB.get(j).getMtrl_prod_id_fk().trim()) 
								&& bis_bom.getId().getMdl_mtrl_seq_id().trim().equals(iaryB.get(j).getMdl_mtrl_seq_id().trim())){
							break;
						}else{
							bisBomDao.delete(bis_bom);
						}
					}
				}
			}
		}
		
		for(FbpmdldefIB iaryB : iaryBList){
			Bis_bomId id = new Bis_bomId();
			
			id.setMdl_id_fk(iaryB.getMdl_id_fk());
			id.setMtrl_prod_id_fk(iaryB.getMtrl_prod_id_fk());
			id.setMdl_mtrl_seq_id(iaryB.getMdl_mtrl_seq_id());
			Bis_bom bis_bom = bisBomDao.get(id);
			if( bis_bom == null){
				Bis_bom bis_bom_new = new Bis_bom();
				id.setMdl_mtrl_seq_id(GUIDGenerator.javaGUID());
				bis_bom_new.setId(id);
				bis_bom_new.setMtrl_cate(iaryB.getMtrl_cate());
				bis_bom_new.setMtrl_unit_fk(iaryB.getMtrl_unit_fk());
				bis_bom_new.setMtrl_use_qty(iaryB.getMtrl_use_qty());
				bis_bom_new.setCns_flg(iaryB.getCns_flg());
				bis_bom_new.setEvt_usr(inTrx.getEvt_usr());
				bis_bom_new.setOpe_id_fk(iaryB.getOpe_id_fk());
				bis_bom_new.setEvt_timestamp(current_timestamp);
				bisBomDao.save(bis_bom_new);
			}else{
				bis_bom.setMtrl_cate(iaryB.getMtrl_cate());
				bis_bom.setMtrl_unit_fk(iaryB.getMtrl_unit_fk());
				bis_bom.setMtrl_use_qty(iaryB.getMtrl_use_qty());
				bis_bom.setCns_flg(iaryB.getCns_flg());
				bis_bom.setOpe_id_fk(iaryB.getOpe_id_fk());
				bis_bom.setEvt_usr(inTrx.getEvt_usr());
				bis_bom.setEvt_timestamp(current_timestamp);
				bisBomDao.update(bis_bom);
			}
		}
		
		/*update产品等级*/

		String hql2 = "FROM Bis_mdl_grd where 1=1 ";
		hql2 = hql2 + " and mdl_id_fk ='" + mdlId + "'";
		List<Bis_mdl_grd> bis_mdl_grds = bisMdlGrdDao.find(hql2);
		
		/**
		 * delete no need port info
		 */
		if(bis_mdl_grds.size()>0){
			List<FbpmdldefIC> iaryC = inTrx.getIary().get(0).getIaryC();
			for(i = 0; i < bis_mdl_grds.size(); i++){
				//数据库里的grds
				Bis_mdl_grd bis_mdl_grd = bis_mdl_grds.get(i);
				if(iaryC.isEmpty()){
					bisMdlGrdDao.delete(bis_mdl_grd);
				}else{
					for(j = 0;j < iaryC.size();j++){
						if(bis_mdl_grd.getMdl_id_fk().trim().equals(mdlId.trim())
								&& bis_mdl_grd.getPrd_grade().trim().equals(iaryC.get(j).getPrd_grade().trim())){
							break;
						}else{
							//1.数据库有，前台没有，删，
							bisMdlGrdDao.delete(bis_mdl_grd);
						}
					}
				}
			}
		}
		//2数据库没有，前台有，增
		for(FbpmdldefIC iaryC : iaryCList){
			Bis_mdl_grd bis_mdl_grd = bisMdlGrdDao.get(iaryC.getEvt_seq_id());
			String cusId = iary.getCus_id();
			String prdGrade = iaryC.getPrd_grade();
			if( bis_mdl_grd == null){
				String  evtSeqId = GUIDGenerator.javaGUID();
				Bis_mdl_grd  bis_mdl_grd_new= new Bis_mdl_grd();
				bis_mdl_grd_new.setEvt_seq_id(evtSeqId);
				bis_mdl_grd_new.setCus_id_fk(cusId);
				bis_mdl_grd_new.setMdl_typ_fk(iary.getMdl_cate());
				bis_mdl_grd_new.setMdl_id_fk(iary.getMdl_id());
				bis_mdl_grd_new.setLow_ng_limt(iaryC.getLow_ng_limt());
				bis_mdl_grd_new.setUp_ng_limt(iaryC.getUp_ng_limt());
//				String gdGrp= getGdGrp(cusId,prdGrade);

//				bis_mdl_grd_new.setGd_grp(gdGrp);
				bis_mdl_grd_new.setPrd_grade(prdGrade);
				bis_mdl_grd_new.setEvt_usr(inTrx.getEvt_usr());
				bis_mdl_grd_new.setEvt_timestamp(current_timestamp);
				bisMdlGrdDao.save(bis_mdl_grd_new);
			}else{
				bis_mdl_grd.setLow_ng_limt(iaryC.getLow_ng_limt());
				bis_mdl_grd.setUp_ng_limt(iaryC.getUp_ng_limt());
//				String gdGrp= getGdGrp(cusId,prdGrade);
//				bis_mdl_grd.setGd_grp(gdGrp);
				bis_mdl_grd.setPrd_grade(iaryC.getPrd_grade());
				bis_mdl_grd.setEvt_usr(inTrx.getEvt_usr());
				bis_mdl_grd.setEvt_timestamp(current_timestamp);
				bisMdlGrdDao.update(bis_mdl_grd);
			}
		}
		
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	
	public void deleteFnc(FbpmdldefI inTrx,FbpmdldefO outTrx) {
		FbpmdldefIA iary = inTrx.getIary().get(0);
		String mdlId = iary.getMdl_id();

		Bis_mdl_def bisMdlDef = bisMdlDefDao.get(mdlId);
		if (bisMdlDef == null) {
			outTrx.setRtn_code(E_READ_NOT_FOUND + "");
			outTrx.setRtn_mesg("not Found Record[BIS_MDL_DEF]");
			return;
		}
		bisMdlDefDao.delete(bisMdlDef);

		String hql = "FROM Bis_bom where mdl_id_fk=?";
		List<Bis_bom> bisBomList = bisBomDao.list(hql, mdlId);
		for (Bis_bom bis_bom : bisBomList) {
			bisBomDao.delete(bis_bom);
		}

		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	public void queryByCondition(FbpmdldefI inTrx,FbpmdldefO outTrx) {
		/***
		 * 暂支持三种查询: 1.by mdl_id 查询 2.by mdl_id 模糊查询 3 by path_ver 4.空 查询所有
		 **/
		FbpmdldefIA iary = inTrx.getIary().get(0);
		String mdlId = iary.getMdl_id();
		String cusId = iary.getCus_id();
		String path_id_fk = iary.getPath_id_fk();
		String validFlg = iary.getValid_flg();
		String hql = "";
		List<FbpmdldefOA> oaryList = new ArrayList<FbpmdldefOA>();

		hql = "FROM Bis_mdl_def where 1=1 ";

		if (mdlId != null) {
			if (mdlId.indexOf("*") == -1) {// 如果不包含*,精确查询
				hql = hql + "and MDL_ID='" + mdlId + "'";
			} else {// 模糊查询
				hql = hql + "and MDL_ID like '" + mdlId.replace("*", "%") + "'";
			}
		}
		if (path_id_fk != null) {
			hql = hql + " and path_id_fk='"+path_id_fk+"'";
		}
		if (cusId != null) {
			hql = hql + " and cus_id='" + cusId + "' ";
		}
		if(validFlg !=null && !validFlg.equals("")){
			hql = hql + " and valid_flg='" + validFlg + "' ";
		}
		List<Bis_mdl_def> bisMdlDefList = bisMdlDefDao.find(hql);

		for (Bis_mdl_def bisMdlDef : bisMdlDefList) {
			FbpmdldefOA oary = new FbpmdldefOA();
			oary.setMdl_id(bisMdlDef.getMdl_id());
			oary.setMdl_cate(bisMdlDef.getMdl_cate());
			oary.setMdl_dsc(bisMdlDef.getMdl_dsc());
			oary.setMdl_size(bisMdlDef.getMdl_size());
			oary.setBox_cnt(bisMdlDef.getBox_cnt());
			oary.setCus_id(bisMdlDef.getCus_id());
			oary.setCus_mdl_id(bisMdlDef.getCus_mdl_id());
			if (bisMdlDef.getFrom_thickness() != null) {
				oary.setFrom_thickness(bisMdlDef.getFrom_thickness());
			}
			if (bisMdlDef.getTo_thickness() != null) {
				oary.setTo_thickness(bisMdlDef.getTo_thickness());
			}
			
			oary.setLayot_id_fk(bisMdlDef.getLayot_id_fk());
			oary.setOk_ratio(bisMdlDef.getOk_ratio());
			oary.setPath_id_fk(bisMdlDef.getPath_id_fk());
			oary.setPath_ver_fk(bisMdlDef.getPath_ver_fk());
			
			oary.setMax_pf_cnt(bisMdlDef.getMax_pf_cnt());
			if(bisMdlDef.getMdl_erp_code()!=null){
				oary.setMdl_erp_code(bisMdlDef.getMdl_erp_code());
			}

			oary.setSwh_time(bisMdlDef.getSwh_time());
			
			oary.setValid_flg(bisMdlDef.getValid_flg());
			oary.setMax_rwk_cnt(bisMdlDef.getMax_rwk_cnt());
			oary.setPost_flag(bisMdlDef.getPost_flag());
			//返回小板尺寸和数量
			Bis_layot bis_layot = bisLayotDao.get(bisMdlDef.getLayot_id_fk());
			if(bis_layot != null){
				Integer x_axis_cnt = bis_layot.getX_axis_cnt();
				Integer y_axis_cnt = bis_layot.getY_axis_cnt();
				if(null != x_axis_cnt && null != y_axis_cnt){
					oary.setX_axis_cnt(x_axis_cnt);
					oary.setY_axis_cnt(y_axis_cnt);
					oary.setLayot_tl_cnt(x_axis_cnt * y_axis_cnt);
				}
			}

			if (mdlId != null && mdlId.indexOf("*") == -1) {
				String hql2 = "FROM Bis_mdl_grd where mdl_id_fk ='" + mdlId + "'";
				List<Bis_mdl_grd> bis_mdl_grds = bisMdlGrdDao.find(hql2);
				if(bis_mdl_grds.size() > 0){
					List<FbpmdldefOB> oaryBList = new ArrayList<FbpmdldefOB>();
					for (Bis_mdl_grd bis_mdl_grd : bis_mdl_grds) {
						FbpmdldefOB oaryb = new FbpmdldefOB();
						oaryb.setEvt_seq_id(bis_mdl_grd.getEvt_seq_id());
						oaryb.setLow_ng_limt(bis_mdl_grd.getLow_ng_limt());
						oaryb.setUp_ng_limt(bis_mdl_grd.getUp_ng_limt());
						oaryb.setMdl_id_fk(bis_mdl_grd.getMdl_id_fk());
						oaryb.setPrd_grade(bis_mdl_grd.getPrd_grade());
						oaryBList.add(oaryb);
					}
					oary.setOaryB(oaryBList);
				}
			}
			oaryList.add(oary);
		}
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());

		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	/**
	 * 获取等级组
	 * @param inTrx
	 * @param outTrx
	 */
	public void queryGradeGroup(FbpmdldefI inTrx,FbpmdldefO outTrx) {
		FbpmdldefIA iary = inTrx.getIary().get(0);
		List<FbpmdldefOA>  oaryList = new ArrayList<>();
		//根据产品代码、等级类型获取等级Group设定
		String sql = "SELECT DISTINCT GD_GRP FROM BIS_MDL_GRD WHERE MDL_ID_FK='" + iary.getMdl_id() + "'";
		if(!Util.isSpaceCheck(iary.getOkng())){
			sql += " AND PRD_JUDGE='" + iary.getOkng() + "'";
		}
		List<String> gd_grps = bisMdlGrdDao.findBySQL(sql);
		if(!gd_grps.isEmpty()){
			for(String gd_grp : gd_grps){
				FbpmdldefOA oary = new FbpmdldefOA();
				oary.setGd_grp(gd_grp);
				oaryList.add(oary);
			}
		}else{
			String sql1 = "SELECT DISTINCT GD_GRP FROM BIS_MDL_GRD WHERE MDL_ID_FK='*'";
			if(!Util.isSpaceCheck(iary.getOkng())){
				sql1 += " AND PRD_JUDGE='" + iary.getOkng() + "'";
			}
			List<String> gd_grps1 = bisMdlGrdDao.findBySQL(sql1);
			for(String gd_grp : gd_grps1){
				FbpmdldefOA oary = new FbpmdldefOA();
				oary.setGd_grp(gd_grp);
				oaryList.add(oary);
			}
		}

		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	
	/**
	 * 获取产品设定等级
	 * @param inTrx
	 * @param outTrx
	 */
	public void queryPrdGrade(FbpmdldefI inTrx,FbpmdldefO outTrx) {
		FbpmdldefIA iary = inTrx.getIary().get(0);
        List<FbpmdldefOA>  oaryList = new ArrayList<>();
        //根据产品代码、等级组、等级类型获取等级等级
		String hql ="FROM Bis_mdl_grd WHERE MDL_ID_FK='" + iary.getMdl_id() + "'";
		if(!Util.isSpaceCheck(iary.getOkng())){
			hql += " AND PRD_JUDGE='" + iary.getOkng() + "'";
		}
        if(!Util.isSpaceCheck(iary.getGd_grp())){
            hql += " and gd_grp = '" + iary.getGd_grp() +"'";
        }
        
		List<Bis_mdl_grd>  bis_mdl_grds =  bisMdlGrdDao.find(hql);
		if(bis_mdl_grds.size()!=0){
            for(Bis_mdl_grd bis_mdl_grd : bis_mdl_grds){
                FbpmdldefOA oary = new FbpmdldefOA();
                oary.setPrd_grade(bis_mdl_grd.getPrd_grade());
				oary.setGrade_desc(bis_mdl_grd.getGrade_desc());
				oary.setPrd_grade_desc(bis_mdl_grd.getPrd_grade()+":"+bis_mdl_grd.getGrade_desc());
                oaryList.add(oary);
            }
        }else{
    		String hql1 ="FROM Bis_mdl_grd WHERE MDL_ID_FK='*'";
    		if(!Util.isSpaceCheck(iary.getOkng())){
    			hql1 += " AND PRD_JUDGE='" + iary.getOkng() + "'";
    		}
            if(!Util.isSpaceCheck(iary.getGd_grp())){
                hql1 += " and gd_grp = '" + iary.getGd_grp() +"'";
            }
            List<Bis_mdl_grd>  bis_mdl_grds1 =  bisMdlGrdDao.find(hql1);
            for(Bis_mdl_grd bis_mdl_grd : bis_mdl_grds1){
                FbpmdldefOA oary = new FbpmdldefOA();
                oary.setPrd_grade(bis_mdl_grd.getPrd_grade());
                oary.setGd_grp(bis_mdl_grd.getGd_grp());
				oary.setGrade_desc(bis_mdl_grd.getGrade_desc());
				oary.setPrd_grade_desc(bis_mdl_grd.getPrd_grade()+":"+bis_mdl_grd.getGrade_desc());
                oaryList.add(oary);
            }
        }

		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	public void queryExistWoIdByPath(FbpmdldefI inTrx,FbpmdldefO outTrx){
		FbpmdldefIA iary  = inTrx.getIary().get(0);
		String pathIdFk = iary.getPath_id_fk();
		String pathVer = iary.getPath_ver_fk();
		String hql = "From Ret_wo where 1 = 1";
		hql = hql + " and path_id_fk = '" + pathIdFk + "'";
		hql = hql + " and path_ver_fk = '" + pathVer + "'";
		hql = hql + " and wo_stat not in ('CLOS')";
		List<Ret_wo> ret_wos = retWODao.find(hql);
			if(ret_wos.size() > 0){
				HashMap<String,String> woNameAndStat = new HashMap<>();
				for(int i = 0; i < ret_wos.size(); i++){
					String woId = ret_wos.get(i).getWo_id();
					String woStat = ret_wos.get(i).getWo_stat();
					woNameAndStat.put(woId,woStat);
				}
				outTrx.setRtn_code(E_READ_NOT_FOUND + "");
				String remesg = "该工艺路线[" + pathIdFk +"],工艺路线版本["+pathVer+"]下存在";

				Iterator iter = woNameAndStat.entrySet().iterator();
				while (iter.hasNext()) {
					Map.Entry entry = (Map.Entry) iter.next();
					String woid = (String)entry.getKey();
					String wostat = (String)entry.getValue();
					remesg = remesg + "工单[" +woid+"],状态为[" + wostat+"] ";
				}
				remesg = remesg + "暂不可更改工艺路线！";
				outTrx.setRtn_mesg(remesg);
				return;
			}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
	}

	public void errActionFnc(FbpmdldefO outTrx) {
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
	}
}
